import { NextRequest } from "next/server";
import { withMiddleware } from "@/lib/middleware/entry";
import { prisma } from "@/lib/prisma";
import { authenticateRequest } from "@/utils/auth";

async function postHandler(req: NextRequest) {
  const authResult = await authenticateRequest(req);
  if (!authResult.success || !authResult.user) {
    throw new Error("请先登录");
  }
  const user = authResult.user;
  
  const { contentId } = await req.json();

  if (!contentId) {
    throw new Error("内容ID不能为空");
  }

  const existingFavorite = await prisma.favorite.findUnique({
    where: {
      userId_contentId: {
        userId: user.id,
        contentId: contentId,
      },
    },
  });

  return { isFavorited: !!existingFavorite };
}

export const POST = withMiddleware(postHandler);